/***
 * * PROJECT
 * *    ATaG
 * * VERSION
 * *    $Revision: 1.3 $
 * * DATE
 * *    $Date: 2008/10/07 12:57:37 $
 * * LOG
 * *    $Log: ComposedATaG.java,v $
 * *    Revision 1.3  2008/10/07 12:57:37  animesh
 * *    removed GPL License [will re-add to all files later]
 * *
 * *    Revision 1.2  2008/10/06 14:03:23  animesh
 * *    removed a random import to bsh.This [must have been included by mistake]
 * *
 * *    Revision 1.1  2007/06/01 20:53:20  animesh
 * *    bulk update.
 * *
 * *
 * *
 * *   ATaG (Abstract Task Graph) - Data-driven Macroprogramming for
 * *                                Sensor Networks
 * *
 ***/

package atag.compiler;

import atag.common.ATaGDataItemDeclaration;
import atag.common.ATaGProgram;
import atag.common.ATaGTaskDeclaration;
import atag.runtime.ln.LNScope;
import java.util.ArrayList;

/**
 * The "Composed" ATaG graph.
 * This contains only tasks, connected by edges labelled with scopes.
 * @author Animesh
 */
public class ComposedATaG {
    
    private ArrayList<CATaGNode> nodes;
    
    /** Creates a new instance of ComposedATaG
     * @params aProgram The ATaG program on which this is based
     */
    public ComposedATaG(ATaGProgram aProgram) {
        nodes = new ArrayList();
        //populate array - note that ID is preserved
        for(ATaGTaskDeclaration st: aProgram.getTaskList()){
            this.nodes.add(new CATaGNode(st));
        }
            
        //for each task in the ATaG Program
        for(ATaGTaskDeclaration st: aProgram.getTaskList()){
            //for each data item that it produces
            for(ATaGDataItemDeclaration d:
                aProgram.getOutDataListOfTask(st.getID())){
                    //for each task that reads this data
                    for(ATaGTaskDeclaration dt:
                        aProgram.getOutTasksListOfData(d.getID())){
                            //link st and dt
                            //add st-d->dt as a new edge to st
                            LNScope newEdge = aProgram.
                                    getLNScopeEdgeForCATaG(st.getID(),d.getID(),dt);
                            
                            this.getCATaGNodeByID(st.getID()).getEdgeList().
                                    add(new CATaGEdge(this.getCATaGNodeByID(dt.getID()),newEdge));
                            
                            
                            //add st to feeder list of dt
                            //what if we are adding the same feeder multiple times?
                            //well.. no problem.. each addition means 
                            //there is a data item for it
                            this.getCATaGNodeByID(dt.getID()).
                                    getFeederList().add(this.getCATaGNodeByID(st.getID()));
                            
                            
                        }
                }
        }
        
    }
    
    public CATaGNode getCATaGNodeByID(int ID){
        //TODO make this ID - independent - do we really need to?
        return this.nodes.get(ID);
    }

    public ArrayList<CATaGNode> getNodeList(){
        return this.nodes;
    }
    
}


